package com.yuanluludev.lib_logcat.logcat;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.Gravity;

import com.yuanluludev.lib_logcat.xtoast.OnToastListener;
import com.yuanluludev.lib_logcat.xtoast.XToast;
import com.yuanluludev.lib_logcat.xtoast.draggable.SpringDraggable;

/**
 * desc   : 悬浮窗口
 */
final class FloatingWindow extends XToast {
    SensorManagerHelper sensorHelper;

    FloatingWindow(Activity activity) {
        super(activity);
        setView(R.layout.logcat_window_floating);
        setAnimStyle(android.R.style.Animation_Toast);
        setDraggable(new SpringDraggable());
        setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
        ((MyView) findViewById(android.R.id.icon)).setMyClickCallBack(new MyView.MyClickCallBack() {
            @Override
            public void oneClick() {
                startActivity(new Intent(getContext(), LogcatActivity.class));
            }

            @Override
            public void doubleClick() {
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
                sp.edit().putBoolean(OnToastListener.LocatKey, false).commit();
                cancel();
            }
        });
        sensorHelper = new SensorManagerHelper(activity);
        sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() {
            @Override
            public void onShake() {
                if (!isShow()) {
                    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
                    sp.edit().putBoolean(OnToastListener.LocatKey, true).commit();
                    show();
                }
            }
        });
    }

    @Override
    public void recycle() {
        sensorHelper.stop();
        super.recycle();
    }
}